<?php

/**
 * Build indices for pressless addons
 */
function pressless_cache_addons()
{
  //
  // Write the XML addons index file.
  //

  $addons = array(); 

  foreach (scandir(PRESSLESS_ADDONS_DIR) as $addon) {
    if ( $addon == '.' or $addon == '..' ) continue;
    $addons[] = $addon;
  }

  $addons_index_file = fopen(PRESSLESS_CACHE_DIR.'/addons.xml', 'w');
  fwrite($addons_index_file, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n");
  fwrite($addons_index_file, '<addons xmlns="http://www.pressless-is-priceless.org/addon" xmlns:xi="http://www.w3.org/2001/XInclude">');
  foreach ($addons as $addon) {
    fwrite($addons_index_file, "  <xi:include href=\"addons/$addon/addon.xml\" />\n");
  }
  fwrite($addons_index_file, "</addons>\n");
  fclose($addons_index_file);

  //
  // Write the PHP addons index file.
  //

  $addons_index_file = fopen(PRESSLESS_CACHE_DIR.'/addons.php', 'w');
  fwrite($addons_index_file, "<?php\n\n");
  foreach ($addons as $addon) {
    fwrite($addons_index_file, "\$pressless_addons[] = '$addon';\n");
  }
  fwrite($addons_index_file, "\n?>\n");
  fclose($addons_index_file);
}


/**
 * Build index of pressless action files.
 */
function pressless_cache_actions()
{
  global $pressless_addons_enabled;

  if (  file_exists(PRESSLESS_CUSTOM_DIR . '/addons-enabled.php')  )
    include_once(PRESSLESS_CUSTOM_DIR . '/addons-enabled.php');

  if (  file_exists(PRESSLESS_CUSTOM_DIR . '/' . PRESSLESS_ENV . '/addons-enabled.php')  )
    include_once(PRESSLESS_CUSTOM_DIR . '/' . PRESSLESS_ENV . '/addons-enabled.php');

  $dirs_to_scan_for_actions = array(PRESSLESS_PRODUCT_DIR.'/actions');
  foreach ($pressless_addons_enabled as $pressless_addon) {
    $pressless_addon_actions_dir = PRESSLESS_ADDONS_DIR."/$pressless_addon/actions";

    if ( file_exists($pressless_addon_actions_dir) )
      $dirs_to_scan_for_actions[] = $pressless_addon_actions_dir;
  }
  $dirs_to_scan_for_actions[] = PRESSLESS_CUSTOM_DIR."/actions";

  $pressless_action_files = array();
    
  foreach ($dirs_to_scan_for_actions as $dir_to_scan_for_actions) {
    foreach (scandir($dir_to_scan_for_actions) as $action_dir) {
      if ( preg_match('/^\./', $action_dir) ) continue;

      $pressless_action_files[$action_dir] = "$dir_to_scan_for_actions/$action_dir";

      foreach(scandir($dir_to_scan_for_actions . '/' . $action_dir) as $action_dir_entry) {
        if ( preg_match('/^\./', $action_dir_entry) ) continue;

        $action_file = $dir_to_scan_for_actions . '/' . $action_dir_entry . '/handler.php';

        $absolute_file_path = realpath("$dir_to_scan_for_actions/$action_dir/$action_dir_entry");

        $relative_file_path = str_replace($dir_to_scan_for_actions . '/', '', $absolute_file_path);

        $pressless_action_files[ $relative_file_path ] = $absolute_file_path;
      }
    }
  }

  //
  // Write the action file list
  //

  $action_index_file = fopen(PRESSLESS_CACHE_DIR.'/actions.php', 'w');
  fwrite($action_index_file, "<?php\n\n");
  foreach ($pressless_action_files as $action => $action_file)
    fwrite($action_index_file, "\$pressless_action_files['$action']='$action_file';\n");
  fwrite($action_index_file, "\n?>\n");
  fclose($action_index_file);
}


function pressless_cache_file_lists()
{
  require_once(PRESSLESS_CACHE_DIR . '/addons.php');

  $main_dirs_to_scan = array(PRESSLESS_PRODUCT_DIR);
  foreach ($pressless_addons as $addon) $main_dirs_to_scan[] = PRESSLESS_ADDONS_DIR."/$addon";
  $main_dirs_to_scan = 
  glob('*', GLOB_ONLYDIR);
}
 
?>
